package com.ithuameng.admin.app.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ithuameng.admin.app.system.dao.DictDataMapper;
import com.ithuameng.admin.app.system.model.DictData;
import com.ithuameng.admin.app.system.model.admin.qo.DictDataQo;
import com.ithuameng.admin.app.system.service.IDictDataService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class DictDataServiceImpl extends ServiceImpl<DictDataMapper, DictData> implements IDictDataService {

    @Override
    public IPage<DictData> listDictData(Page<DictData> page, DictDataQo dictDataQo) {
        QueryWrapper<DictData> queryWrapper = new QueryWrapper<>();
        if (dictDataQo != null) {
            if (StringUtils.isNotBlank(dictDataQo.getDictLabel())) {
                queryWrapper.like(DictData.DICT_LABEL, dictDataQo.getDictLabel());
            }
            if (StringUtils.isNotBlank(dictDataQo.getDictType())) {
                queryWrapper.like(DictData.DICT_TYPE, dictDataQo.getDictType());
            }
            if (dictDataQo.getStatus() != null) {
                queryWrapper.eq(DictData.STATUS, dictDataQo.getStatus());
            }
        }
        return super.page(page, queryWrapper);
    }

    @Override
    public String selectDictLabel(String dictType, String dictValue) {
        QueryWrapper<DictData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(DictData.DICT_VALUE, dictValue);
        queryWrapper.eq(DictData.DICT_TYPE, dictType);
        List<DictData> dictDataList = super.list(queryWrapper);
        if (dictDataList.size() > 0) {
            DictData dictDataDO = dictDataList.get(0);
            return dictDataDO.getDictLabel();
        }
        return null;
    }
}
